package com.wd.mylibrary_my.view.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.net.Uri
import android.opengl.Visibility
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import coil.load
import com.blankj.utilcode.util.UriUtils
import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_my.R
import com.wd.mylibrary_my.databinding.ActivityImageInfoBinding
import com.wd.mylibrary_my.viewmodel.ImageInfoViewModel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ImageInfoActivity : BaseActivity<ImageInfoViewModel,ActivityImageInfoBinding>(R.layout.activity_image_info) {

    override fun initView() {

        //获取 sp 存储的图片
        var headPic = MyApp.sp.getString("headPic", "")

        //赋值获取到的图片
        dataBinding.headPic.load(headPic)


        //返回按钮
        dataBinding.back.setOnClickListener({
            finish()
        })

        //选择 图片
        dataBinding.changeHeadPic.setOnClickListener({
            dataBinding.huan.visibility=View.VISIBLE
        })

        dataBinding.qu.setOnClickListener({
            dataBinding.huan.visibility=View.GONE
        })

        //调用相机
//        dataBinding.camera.setOnClickListener({
//            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
//                checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
//            } else {
//                takePhoto()
//            }
//        })

        //调用图库
        dataBinding.album.setOnClickListener({
            startActivityForResult(Intent(Intent.ACTION_PICK).apply {
                type="image/*"
            },100)
        })


    }

    //调用相机
//    private fun takePhoto() {
//        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
//        startActivityForResult(intent, 1)
//    }

    private var file: File?=null
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && data!=null) {

            //显示头像
            dataBinding.headPic.load(data?.data)

            file= UriUtils.uri2File(data?.data)

            var body= RequestBody.create("multipart/form-data".toMediaTypeOrNull(),file!!)
            var image=  MultipartBody.Part.createFormData("image",file?.name,body)


            var userId = MyApp.sp.getInt("userId", 0)
            var sessionId = MyApp.sp.getString("sessionId", "").toString()


            viewModel.getImageInfoData(userId,sessionId,image)
            Toast.makeText(this,"更换头像成功", Toast.LENGTH_SHORT).show()

            MyApp.sp.edit().putString("headPic",data?.data.toString()).commit()
        }

//        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
//            val imageBitmap = data.extras?.get("data") as Bitmap?
//            if (imageBitmap != null) {
////                val photoUri = saveImageToExternalStorage(imageBitmap)
//            }
//        }
    }

    override fun dataObserve() {
        viewModel.imageInfoData.observe(this,{
            if(it.result!=null){
                if(it.status=="0000"){
                    Toast.makeText(this,"更换头像成功", Toast.LENGTH_SHORT).show()
                }
            }
        })
    }
}